package com.lxk.mall.pms.controller.app;

import cn.hutool.core.collection.CollUtil;
import com.lxk.mall.pms.controller.app.vo.AppCategoryRespVO;
import com.lxk.mall.pms.domain.PmsProductCategory;
import com.lxk.mall.pms.service.IPmsProductCategoryService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.MapstructUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;


/**
 * 商品分类
 */
@RestController
@RequestMapping("/product/category")
@Validated
@RequiredArgsConstructor
public class AppCategoryController {

    private final IPmsProductCategoryService categoryService;

    /**
     * 商品分类列表-全部
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<AppCategoryRespVO>> getProductCategoryList() {
        List<PmsProductCategory> list = categoryService.getEnableCategoryList();
        list.sort(Comparator.comparing(PmsProductCategory::getSort));
        if (CollUtil.isEmpty(list)) {
            return R.ok();
        }
        List<AppCategoryRespVO> categoryList = MapstructUtils.convert(list, AppCategoryRespVO.class);
        return R.ok(categoryList);
    }

    /**
     * 商品分类列表根据编号
     *
     * @param ids 商品分类编号数组
     * @return
     */
    @GetMapping("/list-by-ids")
    public R<List<AppCategoryRespVO>> getProductCategoryList(@RequestParam("ids") List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return R.ok(Collections.emptyList());
        }
        List<PmsProductCategory> list = categoryService.getEnableCategoryList(ids);
        if (CollUtil.isEmpty(list)) {
            return R.ok();
        }
        List<AppCategoryRespVO> categoryList = MapstructUtils.convert(list, AppCategoryRespVO.class);
        return R.ok(categoryList);
    }

}
